﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Data.ConnectionUI;
namespace KeelKit.Controls
{
    public partial class BuildConnectString : UserControl
    {
        public BuildConnectString()
        {
            InitializeComponent();
        }
        DataConnectionDialog dcd ;
        private void btnBuild_Click(object sender, EventArgs e)
        {         
            try
            {
                dcd = new DataConnectionDialog();
                //添加数据源
                AddDS(DataSource.AccessDataSource);
                AddDS(DataSource.OdbcDataSource);
                AddDS(DataSource.OracleDataSource);
                AddDS(DataSource.SqlDataSource);
                AddDS(DataSource.SqlFileDataSource);
                if (csDataSource != null)
                {
                    //指定默认选择数据源 用来还原 链接字符串到DataConnectionDialog中
                    dcd.SelectedDataSource = this.csDataSource ;
                    dcd.SelectedDataProvider = dcd.SelectedDataSource.DefaultProvider;//貌似必须赋值来自dcd.SelectedDataSource的提供器 ，当然，你可以可以取this.csDataSource.DefaultProvider,确保父子关系就可以了。否则链接字符串赋值时会出错
                }
                dcd.ConnectionString = this.txtCntStr.Text;//给链接字符串赋值
            
            }
            catch (Exception)
            {

            }
            DialogResult dr = DialogResult.Cancel  ;
            try
            {  
              dr=  DataConnectionDialog.Show(dcd);
            }
            catch (Exception)
            {

            }
            if (dr == DialogResult.OK)
            {
                //保存值
                this.txtCntStr.Text = dcd.ConnectionString;
                csDataSource = dcd.SelectedDataSource;
                csDataProvider  = dcd.SelectedDataProvider;
 
            }
        }

        private void AddDS(DataSource ds)
        {
            if (csDataSource != null && ds.DisplayName == csDataSource.DisplayName)
            {
                foreach (var item in ds.Providers )
                {
                    if (item.Name == csDataProvider.Name)
                    {
                        DataProvider dp = item;
                        ds.Providers.Clear();
                        ds.Providers.Add(dp);
                        csDataProvider = dp;
                        csDataSource = ds;
                        break;
                    }
                }
            }
            dcd.DataSources.Add(ds);
        }
        public string ConnectionString
        {
            get { return this.txtCntStr.Text; }
            set { this.txtCntStr.Text = value; }
        }
        public DataSource  csDataSource { get; set; }
        public DataProvider csDataProvider { get; set; }
    }
}
